<?php
/**
 * Table View Helper
 */
class Zend_View_Helper_TableForm extends Zend_View_Helper_Abstract
{

    protected $_obj;
    
    protected $_columns = array();
    
    protected $_columnsbyname = array();

    protected $_status = array();
    protected $_priority = array();
    
    protected $_userCareer = array();
    
    protected $_actionStatus;
    protected $_actionEdit;
    protected $_actionDelete;
    
    protected $_footer = array();
    
    protected $_attributes;
	
    public $_numberColumn;
    
    public function tableForm($obj)
    {
        $this->_obj = $obj;
        $this->_numberColumn = 0;
    	return $this;
    }
	
    public function setRows($obj,$key,$NameOfGet=0)
    {
    	$getFuntion = 'get'.$key;
    	$getFuntionGet = 'get'.$NameOfGet;
    	
    	$objArray = array();
    	
    	$numberRows = 0;
    	
    	$this->setColumns($key);
    	
    	foreach ($obj as $object)
    	{
    	   $string = "";
    	   if ($NameOfGet=='0')
    	   {
    	   		if(strlen ($object->$getFuntion()) > 30  )
    	   		    $string = substr($object->$getFuntion(),0,30).'...';
    	   		else 
    	   		    $string = $object->$getFuntion();
    	   }
    	   else 
    	   {
    	       if(strlen ($object->$getFuntion()->$getFuntionGet()) > 30  )
    	       		$string = substr($object->$getFuntion()->$getFuntionGet(),0,30).'...';
    	       else
    	       		$string = $object->$getFuntion()->$getFuntionGet();
    	   }
    	   $objArray[$numberRows]= $string;
    	   $numberRows++;
    	}
    	return $objArray;
    }
    
    public function setColumnByName($NameColumn,$NameOfGet=0)
    {
        $this->_columnsbyname[$this->_numberColumn] = array();
        $this->_columnsbyname[$this->_numberColumn] = $this->setRows($this->_obj, $NameColumn,$NameOfGet);
        return $this;
    }
    
    public function setUserCareer()
    {
        $k=0;
        $this->setColumns('UserCareer');
    	foreach ($this->_obj as $Object)
    	{
    	   	$this->_userCareer[$k]=$Object->getUserCareer();
    		$k++;
    	}
    	return $this;
    }
    
    public function setStatus($actionStatus)
    {
        $this->_actionStatus = $actionStatus;
        $this->_status = $this->setRows($this->_obj, 'Status');
        return $this;
    }
    
    public function setPriority()
    {
    	$this->_priority = $this->setRows($this->_obj, 'Priority');
    	return $this;
    }
    
    public function setActionEdit($action)
    {
        $this->setColumns('');
        $this->_actionEdit = $action;
        return $this;
    }
    public function setActionDelete($action)
    {
    	$this->_actionDelete = $action;
    	return $this;
    }
    
    public function setColumns($column)
    {
        $this->_columns[$this->_numberColumn] = $column;
        $this->_numberColumn = $this->_numberColumn + 1;
        return $this;
    }

    public function setFooter($footer)
    {
        $this->_footer = $footer;
        return $this;
    }

    public function setAttributes(array $attribs)
    {
        $this->_attributes = $attribs;
        return $this;
    }
	
    public function getPrefix($prefix = '')
    {
    	return $prefix;
    }
   
    public function toString()
    {
        $xhtml = $this->getPrefix();

        $xhtml .= '<table ';
        if (count($this->_attributes)) {
            foreach ($this->_attributes as $key=>$value) {
                $xhtml .= $key.'="'.$value.'" ';
            }
        }
        $xhtml .= '>';

        if (count($this->_columns)) {
            $xhtml .= '<thead>';
            
            $xhtml .= '<tr>';
            $bIsFirst = true;
            foreach ($this->_columns as $key=>$value) {
                $xhtml .= '<td class="first_column">'.$value.'</td>';
            }
            $xhtml .= '</tr>';
            $xhtml .= '<tr>';
            foreach ($this->_columns as $key=>$value) {
            	$xhtml .= '<td><input class="search-form-table" type="text" name="search_'.$value.'_'.$this->_actionStatus.'" /></td>';
            }
            $xhtml .= '</tr>';
            $xhtml .= '</thead>';
        }

        $xhtml .= '<tbody>';
		
        if (count($this->_columnsbyname) > 0) {
           
            for ($j = 0; $j < count($this->_columnsbyname[0]);$j++)
            {
	            $xhtml .='<tr>';
				for ($i = 0 ; $i < count($this->_columnsbyname) ;$i++)
				{	
				    $xhtml .='<td>'.$this->_columnsbyname[$i][$j].'</td>';
				}
				if (isset($this->_columnsbyname[1][$j]))
					$xhtml .=$this->UserCareer($j,$this->_columnsbyname[1][$j]);
				
				if (count($this->_status) > 0)
				{
				    $classStatus = ($this->_status[$j] == 1)?'on':'off' ;
				    $xhtml .='<td><div class="status" style="float:left;" ><span class="'.$classStatus.'" id="status_'.$this->_actionStatus.'_'.$this->_columnsbyname[0][$j].'" ></span></div></td>';     
				}
				    
				if (count($this->_priority)>0)
				{       
				    $xhtml .='<td><select disabled="disabled" name="priority" id="priority_'.$this->_actionStatus.'_'.$this->_columnsbyname[0][$j].'" >';
				    $xhtml .='<option value="1"'.$this->checkselected($this->_priority[$j],'1' ).'> Top </option>';
				    $xhtml .='<option value="2"'.$this->checkselected($this->_priority[$j],'2' ).'> Height </option>';
				    $xhtml .='<option value="3"'.$this->checkselected($this->_priority[$j],'3' ).'> Medium </option>';
				    $xhtml .='<option value="4"'.$this->checkselected($this->_priority[$j],'4' ).'> Low </option>';
				    $xhtml .='</select>';
				    $xhtml .='<a class="help" name="enable_priority" id="enable_priority_'.$this->_actionStatus.'_'.$this->_columnsbyname[0][$j].'" >';
				    $xhtml .=	'<span id="icon_edit_priority_'.$this->_columnsbyname[0][$j].'" class="icon-button edit" > Edit Priority</span>';
				    $xhtml .=	'<span class="bullet" style="width: 60px;" >Edit Priority</span>';
				    $xhtml .=	'<span class="icon-button ui-icon-triangle-1-s queue" ></span>';
				    $xhtml .='</a>';
				    $xhtml .='</td>';
				}
				    
				if ($this->_actionEdit)
				{
				    $xhtml .='<td><a class="help" href="'.$this->_actionEdit.'/id/'.$this->_columnsbyname[0][$j].'">';
				    $xhtml .='<span class="icon-button edit" > Edit </span>
				    <span class="bullet" style="width: 24px;" >Edit</span>
				    <span class="icon-button ui-icon-triangle-1-s queue" ></span></a>';
				}
				    
				if ($this->_actionDelete)
				{
				 	$xhtml .='<a class="help" href="'.$this->_actionDelete.'/id/'.$this->_columnsbyname[0][$j].'">';
				  	$xhtml .='<span class="icon-button delete" > Delete </span>
				   	<span class="bullet" style="width: 34px;" >Delete</span>
				   	<span class="icon-button ui-icon-triangle-1-s queue" ></span></a>';
				}
				    
				$xhtml .='</td>';
				$xhtml .='</tr>';
			}
	   	}
        else {
            $xhtml .= '<tr><td class="first_column" colspan="'.count($this->_columns).'">'.$this->_emptyRowContent.'</td></tr>';
        }

        $xhtml .= '</tbody>';

        if ($this->_footer) {
            $xhtml .= '<tfoot>';
            $xhtml .= '<tr>';
           
            $xhtml .= '</tr>';
            $xhtml .= '</tfoot>';
        }
        $xhtml .= '</table>';

        return $xhtml;
    }
    private  function UserCareer($j,$NameCareer)
    {
        $xhtml ='';
        $i=0;
        if (count($this->_userCareer)>0)
        {
        	if(count($this->_userCareer[$j])>0){
        		$xhtml .='<td><a name="career_popup" id="career_popup_'.$j.'" class="help">';
        		$xhtml .='<span> <image src="/images/admin/user_career_red.png" /> </span>
        		<span class="bullet" style="width: 44px;" >Any One</span>
        		<span class="icon-button ui-icon-triangle-1-s queue" ></span></a>';
        		$xhtml .='<div id="career_popup_content_'.$j.'"  style="display:none;" ><span style="font-size:18px;" > Career :'.$NameCareer.'  </span>';
        		$xhtml .=	'<table class="table">';
        		$xhtml .=		'<thead><tr><td>Id</td><td>Name</td><td>Email</td><td>Aplication</td><td>Coverleter</td><td>Send Email</td></tr></thead>';
        		foreach ($this->_userCareer[$j] as $userCareer)
        		{
        			$xhtml .='<tr>	<td>'.$userCareer->getId().'</td>
        			<td class="tooltip" name="tooltip_career_'.$j. $i.'">'.$userCareer->getUserDetail()->getFirstname().' '.$userCareer->getUserDetail()->getLastname().'</td>
        			<td>'.$userCareer->getUserDetail()->getEmail().'</td>
        			<td name="'.$userCareer->getApplication().'" class="user-career-download" ></td>
        			<td name="'.$userCareer->getCoverletter().'" class="user-career-download" ></td>
        			<td><a href="mailto:'.$userCareer->getUserDetail()->getEmail().'?subject=Interview From HAE" target="_blank" ><image src="/images/admin/send_email.png" /></a></td>'.
        			'<div style="display:none;" id="tooltip_career_'.$j. $i.'" >'.
        				'<div class="tooltip_career_image" ><img src="/files/images/career/'.$userCareer->getUserDetail()->getImage().'  "></div>'.
        				'<div class="tooltip_career_info">'.
        					'<span><label> Address : </label>'.$userCareer->getUserDetail()->getAddress().'</span><br/>'.
        					'<span><label> BirthDay : </label>'.$this->convertDate2String($userCareer->getUserDetail()->getBirthdate()).'</span><br/>'.
        					'<span><label> Phone Number : </label>'.$userCareer->getUserDetail()->getPhonenumber().'</span><br/>'.
        					'<span><label> Email : </label>'.$userCareer->getUserDetail()->getEmail().'</span><br/>'.
        					'<span><label> Country : </label>'.$userCareer->getUserDetail()->getCountry().'</span>'.
        			'</div>'.
        			'</tr>';
        			$i++;
        		}
        		$xhtml .=	'</table>';
        		$xhtml .='</div>';
        		$xhtml .='</td>';
        	}
        	else
        	{
        		$xhtml .='<td><a class="help">';
        		$xhtml .='<span ><image src="/images/admin/user_career_black.png" /></span>
        		<span class="bullet" style="width: 44px;" >No One</span>
        		<span class="icon-button ui-icon-triangle-1-s queue" ></span></a></td>';
        	}
        }
        return $xhtml;
    }
    private function convertDate2String($inputDate) {
        if ($inputDate)
        {
	        $date = get_object_vars($inputDate);
	        $date =  explode(" ",$date['date']);
	        return (string)$date[0];
        }
        else 
            return '';
    }
	private  function checkselected($priority,$i)
	{
		if ($priority == $i)
		    return 'selected="selected"';
		else
		    return '';
	}
    private function _replacePlaceholders($matches)
    {
        return (isset($this->_currentRow[$matches[1]])) ? $this->_currentRow[$matches[1]] : '';
    }

}
?>